class FsetsController < ApplicationController
  # GET /fsets
  # GET /fsets.xml
  
  before_filter :clear_game
  before_filter :check_logged_in, :only => [:destroy, :update, :create, :edit, :new, :fplay, :until_correct, :just_wrong, :all_cards]
  before_filter :has_stats, :only => [:stats]


  
  # 
  def has_stats
	@fset = Fset.find(params[:id])
	@games = @fset.get_stats (current_user)
	if @games.length == 0
		redirect_to(stats_none_fset_path(@fset)) 
	end 
  end 
  
  def index
    if logged_in?  
      @my_fsets = current_user.fsets
	    @my_fsets = @my_fsets.collect {|set| set.name = set.name + " (" + set.flashcards.count.to_s + ")"; set}
      @all_fsets = Fset.paginate(:all, :page => params[:page], :conditions => ["user_id != ? and public = ?", current_user.id, true])
    else
      @all_fsets = Fset.paginate(:all, :page => params[:page], :conditions => ["public = ?", true])
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @fsets }
    end
  end

  # GET /fsets/1
  # GET /fsets/1.xml
  def show
    @fset = Fset.find(params[:id])
    @flashcards = @fset.flashcards
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @fset }
    end
  end

  # GET /fsets/new
  # GET /fsets/new.xml
  def new
    @fset = Fset.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @fset }
    end
  end

  # GET /fsets/1/edit
  def edit
    @fset = Fset.find(params[:id])
    @flashcards = @fset.flashcards
    @new_flashcard = Flashcard.new
    @tags = @fset.tag_list
  	
    if (@fset.user != current_user) 
      access_denied
    end
  end

  # POST /fsets
  # POST /fsets.xml
  def create
    @fset = Fset.new(params[:fset])
#	@fset.public = true
    @fset.user = current_user
#    @flashcards = @fset.flashcards
#    @new_flashcard = Flashcard.new

    respond_to do |format|
      if @fset.save
        flash[:notice] = 'Set was successfully created.'
        format.html { redirect_to(edit_fset_path(@fset)) }
#        format.html { render :action => "edit" }
        format.xml  { render :xml => @fset, :status => :created, :location => @fset }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @fset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /fsets/1
  # PUT /fsets/1.xml
  def update
    @fset = Fset.find(params[:id])
    if (@fset.user != current_user) ###this repeats code from edit.  I feel dirty.
      access_denied
    end

    respond_to do |format|
      if @fset.update_attributes(params[:fset])
        flash[:notice] = 'Set was successfully updated.'
        format.html { redirect_to(edit_fset_path(@fset)) }
#        format.html { render :action => "edit" }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @fset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /games/1
  # PUT /games/1.xml
  def fplay
    @fset = Fset.find(params[:id])
	session[:game_format] = 0
	respond_to do |format|
        format.html { redirect_to(new_fset_game_path(@fset)) }
    end
  end
  
  def search
	@tags = Fset.tag_counts
	@results = Fset.find_tagged_with(params[:tag])
    @results = @results.collect {|set| set.name = set.name + " (" + set.flashcards.count.to_s + ")"; set}
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @fsets }
    end

  end

  
  
  # PUT /games/1
  # PUT /games/1.xml
  def add_tag
    @fset = Fset.find(params[:id])
	@fset.tag_list.add(params[:fset][:tag_list].gsub(" ", "_")) 
	@fset.save
	#tag = params[:tag_list]
	#	@fset.update_attributes(params[:fset])
#	flash[:notice] = 'Tag Added'
	render :update do |page|
	  # I'm a bad bad bad person.  I apologize for this atrocity.  I blame the ajax.
	  tag = params[:fset][:tag_list].gsub(" ", "_")
    l1 = "<span id='tag_"+tag+"'>"
    l2 = tag
	  l3 = link_to_remote 'DELETE', :url => {:action => "delete_tag", :fset => @fset, :tag => tag}, :html => { :class=>'tag_delete_link', :onclick=>'document.getElementById(\'tag_'+tag+'\').style.display="none"'}
    l4 = "</span>"
    page.insert_html :bottom, "tags", l1+l2+l3+l4
#	respond_to do |format|
#        format.html { redirect_to(stats_fset_path(@fset)) }
#        format.js {  }
    end
  end
  
  def delete_tag
    @fset = Fset.find(params[:fset])
    puts params[:tag]
    @fset.tag_list.remove(params[:tag])
    @fset.save!
    render :update do |page|
    end
  end
  
  
  # PUT /games/1
  # PUT /games/1.xml
  def all_cards
    @fset = Fset.find(params[:id])
	session[:game_format] = 0
	respond_to do |format|
        format.html { redirect_to(new_fset_game_path(@fset)) }
    end
  end
  
  # PUT /games/1
  # PUT /games/1.xml
  def just_wrong
    @fset = Fset.find(params[:id])
	session[:game_format] = 1
	respond_to do |format|
        format.html { redirect_to(new_fset_game_path(@fset)) }
    end
  end
  
  # PUT /games/1
  # PUT /games/1.xml
  def until_correct
    @fset = Fset.find(params[:id])
	session[:game_format] = 2
	respond_to do |format|
        format.html { redirect_to(new_fset_game_path(@fset)) }
    end
  end
  
  
  # GET /games/1/play
  # GET /games/1/play.xml
  def stats
	@card_stats = CardScore.find(:all, 	
									:joins => :flashcard, 
									:select => "card_scores.flashcard_id, card_scores.times_correct, card_scores.times_incorrect",
									:conditions => ["flashcards.fset_id = ? ", @fset.id])	
	@graph_info_cards = CardScore.prepare_graph(@card_stats)
	@graph_info = Game.prepare_graph(@games)
 	@last_score = @games.last.score
    @out_of = @games.last.score + @games.last.wrong_count
	@tags = @fset.tag_list
	respond_to do |format|
      format.html # play.html.erb
      format.xml  { render :xml => @game }
    end
  end
  
  # GET /games/1/play
  # GET /games/1/play.xml
  def stats_none
  	@fset = Fset.find(params[:id])
	respond_to do |format|
      format.html # play.html.erb
      format.xml  { render :xml => @game }
    end
  end
  
  # DELETE /fsets/1
  # DELETE /fsets/1.xml
  def destroy
    @fset = Fset.find(params[:id])
    if (@fset.user != current_user) ###this repeats code from edit.  I feel dirty.
      access_denied
    else
      @fset.destroy
    
      respond_to do |format|
        format.html { redirect_to(fsets_url) }
        format.xml  { head :ok }
      end
    end
  end
  
  def rate
    @fset = Fset.find(params[:id])
    @fset.rate(params[:stars], current_user)
    id = "ajaxful-rating-fset-#{@fset.id}" 
    render :update do |page|
      page.replace_html id, ratings_for(@fset, :static)
      #page.insert_html :bottom, id, "Thanks for rating!" 
      #page.visual_effect :highlight, id
    end
  end
  
  def toggle_public_private
    @fset = Fset.find(params[:id])
    if @fset.public
      @fset.public = false
    else
      @fset.public = true
    end
    @fset.save!
    if @fset.public
      render :update do |page|
        #link_to_remote("Make Private", :update => "private", :url => { :action => "toggle_public_private", :id => @fset.id })
      end
    else
      render :update do |page|
        #page.replace_html "private", link_to_remote("Make Public", :update => "private", :url => { :action => "toggle_public_private", :id => @fset.id })
      end
    end
  end
end
